﻿using System;
using System.Linq;

namespace PropertyExpression.CalcExpression.Test
{
    internal class Program
    {
        private static void AddEntityB(EntityA entityA)
        {
            int maxId = int.MinValue / 2;
            foreach (EntityB entityB in entityA.EntityBs)
            {
                if (maxId < entityB.Id)
                {
                    maxId = entityB.Id;
                }
            }

            EntityB entityB2 = new EntityB
                                   {
                                       Id = (maxId + 1),
                                       Price = 3,
                                       Quantity = 1
                                   };

            entityA.EntityBsWrapper.Add(entityB2);
        }

        private static void Main()
        {
            var dataContext = new BusinessObjectsDataContext();

            EntityA entityA = dataContext.EntityAs.Single(x => x.Id == 2);

            var propertyValue = entityA.Val(x => x.TotalSum);

            Console.WriteLine(propertyValue);

            entityA.TotalSum = 555.23m;

            Console.WriteLine(propertyValue);
            Console.WriteLine(propertyValue);

            entityA.TotalSum = 753;

            Console.WriteLine(propertyValue);

            Console.WriteLine(propertyValue.Add(propertyValue.Mult(propertyValue)));

            var resultValue = propertyValue.Func(propertyValue, (x, y) => x * y).Func(propertyValue, (x, y) => x + 3)
                .Func(propertyValue, propertyValue, (x, y, z) => x + y + z);

            Console.WriteLine(resultValue);
            Console.WriteLine(resultValue);

            entityA.TotalSum = 2;

            Console.WriteLine(resultValue);

            foreach (EntityB entityB in entityA.EntityBs)
            {
                Console.WriteLine(entityB.Price);
            }

            var sum2 = entityA.EntityBsWrapper.Sum(
                entity => entity.Val(x => x.Price).Add(entity.Val(x => x.Price))
                );

            Console.WriteLine(sum2);

            AddEntityB(entityA);

            Console.WriteLine(sum2);
            Console.WriteLine(sum2);

            foreach (EntityB entityB in entityA.EntityBs)
            {
                entityB.Price += 1;
                break;
            }

            Console.WriteLine(sum2);

            EntityB entityB2 = entityA.EntityBs.AsEnumerable().ElementAt(0);

            var val = entityB2.Val(x => x.EntityA).Val2(x => x.TotalSumHolder);

            Console.WriteLine("**************");

            Console.WriteLine(val);

            EntityA entityA2 = dataContext.EntityAs.Single(x => x.Id == 3);
            entityA2.TotalSum = 78;
            entityB2.EntityA = entityA2;

            Console.WriteLine(val);

            entityA2.TotalSum = 555;

            Console.WriteLine(val);

            entityB2.Quantity = 3;

            new CalcBinder().Bind(x => entityB2.Price = x, entityB2.Val(x => x.Quantity).Func(x => x*2));

            entityB2.Quantity = 4;
            Console.WriteLine(entityB2.Price);

            entityB2.Quantity = 5;
            Console.WriteLine(entityB2.Price);


            BusinessObjects2DataContext businessObjects2DataContext = new BusinessObjects2DataContext();

            Aaa aaa = new Aaa();
            businessObjects2DataContext.Aaas.InsertOnSubmit(aaa);

            Console.WriteLine(aaa.A002);

            aaa.A001 = 5;

            Console.WriteLine(aaa.A002);

            aaa.A001 = 7;

            Console.WriteLine(aaa.A002);

        }
    }
}